package com.cyzy.controller;

import com.cyzy.dto.BalancePayDto;
import com.cyzy.dto.MyAllAdoptOrderDto;
import com.cyzy.dto.OrderCreateDto;
import com.cyzy.dto.Result;
import com.cyzy.pojo.Orders;
import com.cyzy.service.OrderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/order")
public class OrderController {
    
    @Autowired
    private OrderService orderService;
    
    @PostMapping("/create")
    public Result createOrder(@RequestBody OrderCreateDto orderCreateDto) {
        try {
            // 参数校验
            if (orderCreateDto.getOrder_num() == null || orderCreateDto.getOrder_num().trim().isEmpty()) {
                return Result.error().message("订单号不能为空");
            }
            if (orderCreateDto.getUser_id() == null) {
                return Result.error().message("用户ID不能为空");
            }
            if (orderCreateDto.getOrder_type() == null || orderCreateDto.getOrder_type().trim().isEmpty()) {
                return Result.error().message("订单类型不能为空");
            }
            if (orderCreateDto.getRelated_id() == null) {
                return Result.error().message("关联ID不能为空");
            }
            if (orderCreateDto.getOrder_amount() == null || orderCreateDto.getOrder_amount() <= 0) {
                return Result.error().message("订单金额必须大于0");
            }
            if (orderCreateDto.getOrder_address_id() == null) {
                return Result.error().message("收货地址不能为空");
            }
            if (orderCreateDto.getOrder_quantity() == null || orderCreateDto.getOrder_quantity() <= 0) {
                return Result.error().message("订单数量必须大于0");
            }
            
            // 创建订单
            Orders order = orderService.createOrder(orderCreateDto);
            
            return Result.ok().data("order", order).message("订单创建成功");
            
        } catch (Exception e) {
            e.printStackTrace(); // 打印完整异常信息，便于调试
            return Result.error().message("创建订单失败：" + e.getMessage());
        }
    }
    
    @PostMapping("/balancePay")
    public Result balancePay(@RequestBody BalancePayDto balancePayDto) {
        try {
            // 参数校验
            if (balancePayDto.getUser_id() == null) {
                return Result.error().message("用户ID不能为空");
            }
            if (balancePayDto.getOrder_num() == null || balancePayDto.getOrder_num().trim().isEmpty()) {
                return Result.error().message("订单号不能为空");
            }
            if (balancePayDto.getOrder_amount() == null || balancePayDto.getOrder_amount() <= 0) {
                return Result.error().message("支付金额必须大于0");
            }
            
            // 执行余额支付
            String result = orderService.balancePay(balancePayDto);
            
            return Result.ok().data("message", result).message("支付成功");
            
        } catch (Exception e) {
            e.printStackTrace(); // 打印完整异常信息，便于调试
            return Result.error().message("支付失败：" + e.getMessage());
        }
    }
    
    @GetMapping("/getUserAdoptOrders/{userId}")
    public Result getUserAdoptOrders(@PathVariable("userId") Long userId) {
        try {
            // 参数校验
            if (userId == null || userId <= 0) {
                return Result.error().message("用户ID不能为空且必须大于0");
            }
            
            // 获取用户认养订单
            List<MyAllAdoptOrderDto> adoptOrders = orderService.getUserAdoptOrders(userId);
            
            return Result.ok().data("adoptOrders", adoptOrders).message("获取用户认养订单成功");
            
        } catch (Exception e) {
            e.printStackTrace(); // 打印完整异常信息，便于调试
            return Result.error().message("获取用户认养订单失败：" + e.getMessage());
        }
    }
    
    @GetMapping("/getUserActiveAdoptOrders/{userId}")
    public Result getUserActiveAdoptOrders(@PathVariable("userId") Long userId) {
        try {
            // 参数校验
            if (userId == null || userId <= 0) {
                return Result.error().message("用户ID不能为空且必须大于0");
            }
            
            // 获取用户认养中的订单
            List<MyAllAdoptOrderDto> activeOrders = orderService.getUserActiveAdoptOrders(userId);
            
            return Result.ok().data("activeOrders", activeOrders).message("获取用户认养中订单成功");
            
        } catch (Exception e) {
            e.printStackTrace(); // 打印完整异常信息，便于调试
            return Result.error().message("获取用户认养中订单失败：" + e.getMessage());
        }
    }
    
    @GetMapping("/getUserCompletedAdoptOrders/{userId}")
    public Result getUserCompletedAdoptOrders(@PathVariable("userId") Long userId) {
        try {
            // 参数校验
            if (userId == null || userId <= 0) {
                return Result.error().message("用户ID不能为空且必须大于0");
            }
            
            // 获取用户已完成的认养订单
            List<MyAllAdoptOrderDto> completedOrders = orderService.getUserCompletedAdoptOrders(userId);
            
            return Result.ok().data("completedOrders", completedOrders).message("获取用户已完成认养订单成功");
            
        } catch (Exception e) {
            e.printStackTrace(); // 打印完整异常信息，便于调试
            return Result.error().message("获取用户已完成认养订单失败：" + e.getMessage());
        }
    }
    
    @GetMapping("/getUserUnpaidAdoptOrders/{userId}")
    public Result getUserUnpaidAdoptOrders(@PathVariable("userId") Long userId) {
        try {
            // 参数校验
            if (userId == null || userId <= 0) {
                return Result.error().message("用户ID不能为空且必须大于0");
            }
            
            // 获取用户待支付的认养订单
            List<MyAllAdoptOrderDto> unpaidOrders = orderService.getUserUnpaidAdoptOrders(userId);
            
            return Result.ok().data("unpaidOrders", unpaidOrders).message("获取用户待支付认养订单成功");
            
        } catch (Exception e) {
            e.printStackTrace(); // 打印完整异常信息，便于调试
            return Result.error().message("获取用户待支付认养订单失败：" + e.getMessage());
        }
    }
    
    @PostMapping("/deleteOrder/{orderId}")
    public Result deleteOrder(@PathVariable("orderId") Long orderId) {
        try {
            // 参数校验
            if (orderId == null || orderId <= 0) {
                return Result.error().message("订单ID不能为空且必须大于0");
            }
            
            // 删除订单（假删除）
            Boolean deleteResult = orderService.deleteOrder(orderId);
            
            if (deleteResult) {
                return Result.ok().message("订单删除成功");
            } else {
                return Result.error().message("订单删除失败");
            }
            
        } catch (Exception e) {
            e.printStackTrace(); // 打印完整异常信息，便于调试
            return Result.error().message("订单删除失败：" + e.getMessage());
        }
    }
    
    @PostMapping("/cancelOrder/{orderId}")
    public Result cancelOrder(@PathVariable("orderId") Long orderId) {
        try {
            // 参数校验
            if (orderId == null || orderId <= 0) {
                return Result.error().message("订单ID不能为空且必须大于0");
            }
            
            // 取消订单
            Boolean cancelResult = orderService.cancelOrder(orderId);
            
            if (cancelResult) {
                return Result.ok().message("订单取消成功");
            } else {
                return Result.error().message("订单取消失败");
            }
            
        } catch (Exception e) {
            e.printStackTrace(); // 打印完整异常信息，便于调试
            return Result.error().message("订单取消失败：" + e.getMessage());
        }
    }
} 